use serde::{Deserialize, Serialize};

/// 链接
#[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)]
pub struct Url {
    pub id: String,
    /// 用户ID
    pub user_id: String,
    /// 原始链接
    pub origin: String,
    /// 短链接
    pub url: String,
    /// 浏览次数
    pub hit: i64,
    /// 是否有密码
    pub has_password: bool,
    /// 密码
    // #[serde(skip_serializing)]
    pub password: String,
    /// 是否有过期时间
    pub has_expired: bool,
    /// 过期时间
    pub expired: chrono::DateTime<chrono::Local>,
    pub dateline: chrono::DateTime<chrono::Local>,
}

/// 链接访问记录
#[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)]
pub struct UrlLogs {
    pub id: String,
    /// 短链接
    pub url: String,
    /// IP地址
    pub ip: String,
    /// 用户代理
    pub user_agent: String,
    pub dateline: chrono::DateTime<chrono::Local>,
}

/// 用于视图的链接
#[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)]
pub struct UrlForView {
    pub url_id: String,
    /// 用户ID
    pub user_id: String,
    /// 原始链接
    pub origin: String,
    /// 短链接
    pub url: String,
    /// 浏览次数
    pub hit: i64,
    /// 是否有密码
    pub has_password: bool,
    /// 密码
    #[serde(skip_serializing)]
    pub password: String,
    /// 是否有过期时间
    pub has_expired: bool,
    /// 过期时间
    pub expired: chrono::DateTime<chrono::Local>,
    pub url_dateline: chrono::DateTime<chrono::Local>,
}

/// 带链接信息的访问记录
#[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)]
pub struct UrlWithLogs {
    #[serde(flatten)]
    #[sqlx(flatten)]
    pub url: UrlForView,

    #[serde(flatten)]
    #[sqlx(flatten)]
    pub log: UrlLogs,
}
